<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-row>
		  <p-col  :span="24">
			<p-form-item label="教材编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'textbookNo', validatorRules.textbookNo]" placeholder="请输入教材编码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="教材名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'textbookName', validatorRules.textbookName]" placeholder="请输入教材名称"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="技能名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="mes-manpower"
						v-decorator="['skillName', validatorRules.skillName]"
						:trigger-change="true"
						org-fields="id,skill_name"
						dest-fields="skill_id,skill_name"
						code="mes_hr_q_skill"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="技能内码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'skillId', validatorRules.skillId]" placeholder="请输入技能内码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'version', validatorRules.version]" placeholder="请输入版本"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-upload v-decorator="['fileUrl', validatorRules.fileUrl]" :trigger-change="true"></j-upload>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: "MesHrTextbookBankModal",
    mixins: [SubmitOnchangMixin],
    components: {
      JUpload,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          textbookNo: {rules: [
            {required: true, message: '请输入教材编码!'},
          ]},
          textbookName: {rules: [
            {required: true, message: '请输入教材名称!'},
          ]},
          skillName: {rules: [
            {required: true, message: '请输入技能名称!'},
          ]},
          skillId: {rules: [
          ]},
          version: {rules: [
            {required: true, message: '请输入版本!'},
          ]},
          fileUrl: {rules: [
          ]},
        },
        url: {
          add: '/mes-manpower/auto/mesHrTextbookBank/add',
          edit: '/mes-manpower/auto/mesHrTextbookBank/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'textbookNo','textbookName','skillName','skillId','version','fileUrl'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'textbookNo','textbookName','skillName','skillId','version','fileUrl'))
      },


    }
  }
</script>